package com.dingding.special.user.setDepositPwd.service.impl;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.code.BaseCodeMessage;
import com.dingding.common.core.base.dto.AbstractValiResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.MD5Util;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.special.user.setDepositPwd.dao.DepositPwdDao;
import com.dingding.special.user.setDepositPwd.dto.DepositPasswordVerifyDTO;
import com.dingding.special.user.setDepositPwd.dto.ReqDepositPwdDTO;
import com.dingding.special.user.setDepositPwd.dto.ResPromptDTO;
import com.dingding.special.user.setDepositPwd.dto.TellPhoneValidateDTO;
import com.dingding.special.user.setDepositPwd.service.DepositPwdService;
import com.dingding.special.user.validate.service.IValidateCodeService;

/**
 * @ClassName: DepositPwdServiceImpl
 * @Description: 专车 - 设置提现密码
 * @author SUNZHICHENG
 * @date 2016年8月25日 下午8:53:04
 * @version 2.4.0
 * 
 */
@Service("depositPwdService")
public class DepositPwdServiceImpl extends AbstractValiResponse implements DepositPwdService {

	@Resource(name = "depositPwdDao")
	DepositPwdDao depositPwdDao;

	@Resource(name = "validateCodeService")
	IValidateCodeService validateCodeService;

	Logger logger = Logger.getLogger(DepositPwdServiceImpl.class);


	/**
	 * 
	 * @Title: setDepositPwd
	 * @Description: 首次设置提现密码
	 * @param ReqDepositPwdDTO
	 * @return ResponseEntity<ResPromptDTO>
	 * @author SUNZHICHENG
	 * @date 2016年8月25日 下午8:45:05
	 * @since v2.4.0
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public ResponseEntity<ResPromptDTO> setDepositPwd(ReqDepositPwdDTO req) {
		ResponseEntity<ResPromptDTO> responseEntity = new ResponseEntity<ResPromptDTO>();
		// 请求参数验证
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(req, ReqDepositPwdDTO.class);
		// 参数不合法，存在空值
		if (!valiResult.isAvailable()) {
			return super.error(valiResult);
		}

		ResPromptDTO resPromptDTO = new ResPromptDTO();
		// 调用dao设置提现密码，
		boolean isSucceed = depositPwdDao.setDepositPwd(req);

		if (isSucceed) {
			// 添加修改状态到响应bean
			resPromptDTO.setIsDeposit(2); // 修改成功
			// 设置http请求状态
			responseEntity.setCode(HttpStatus.SC_OK);
			responseEntity.setPromptMessage("请求成功");
		} else {
			resPromptDTO.setIsDeposit(1); // 修改失败
			responseEntity.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			responseEntity.setErrorMessage(BaseCodeMessage.SERVER_ERROR);
		}
		responseEntity.setResponseBody(resPromptDTO);

		return responseEntity;
	}

	/**
	 * 
	 * @Title: getSpecialTellPhoneValidate
	 * @Description: 专车 - 手机验证码校验
	 * @param TellPhoneValidateDTO
	 * @return ResponseEntity<ResPromptDTO>
	 * @author SUNZHICHENG
	 * @date 2016年8月29日 下午1:30:02
	 * @since v2.4.0
	 * 
	 */
	@SuppressWarnings("unused")
	@Override
	public ResponseEntity<ResPromptDTO> getSpecialTellPhoneValidate(TellPhoneValidateDTO tellPhoneValidateDTO) {
		ResponseEntity<ResPromptDTO> responseEntity = new ResponseEntity<ResPromptDTO>();
		// 请求参数验证
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(tellPhoneValidateDTO, TellPhoneValidateDTO.class);
		// 参数不合法，存在空值
		if (!valiResult.isAvailable()) {
			return super.error(valiResult);
		}
		// 校验短信验证码
		boolean flag = validateCodeService.telPhoneCodeValidate(tellPhoneValidateDTO.getUserName(),
				tellPhoneValidateDTO.getValidateNum());
		ResPromptDTO resPromptDTO = new ResPromptDTO();
		if (flag) {
			
			responseEntity.setPromptMessage("请求成功");

		} else {
			//responseEntity.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			responseEntity.setErrorMessage("验证失败");
			
		}
		// responseEntity.setResponseBody(resPromptDTO);
		return super.success(HttpStatus.SC_OK);
	}

	/**
	 * 
	 * @Title: DepositPasswordVerify 
	 * @Description: 验证提现密码
	 * @param depositPasswordVerifyDTO
	 * @return ResponseEntity<ResPromptDTO> 
	 * @author SUNZHICHENG  
	 * @date  2016年8月29日 下午7:54:02
	 * @since v2.4.0
	 */
	@SuppressWarnings("unused")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public ResponseEntity<ResPromptDTO> DepositPasswordVerify(DepositPasswordVerifyDTO depositPasswordVerifyDTO) {
		ResponseEntity<ResPromptDTO> response=new ResponseEntity<ResPromptDTO>();
		
		//验证请求参数
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(depositPasswordVerifyDTO, DepositPasswordVerifyDTO.class);
		//请求参数为空
		if (!valiResult.isAvailable()) {
			return super.error(valiResult);
		}
		//根据用户id查询账户信息
		Specialuserlogin specialuserlogin = depositPwdDao.DepositPasswordVerify(depositPasswordVerifyDTO);
		
		if (specialuserlogin != null) {
			//用户名是否匹配
			if (specialuserlogin.getUserName().equals(depositPasswordVerifyDTO.getUserName()) ) {
				//密码比对
				if (specialuserlogin.getDepositPwd() .equals( MD5Util.md5Encrypt(depositPasswordVerifyDTO.getDepositPwd()))) {
					return super.success(HttpStatus.SC_OK);
				}else{
					//密码错误
					return super.error(SystemCode.WITHDRAWALS_FAIL_ERROR, SystemCode.WITHDRAWALS_FAIL_MESSAGE, SystemCode.WITHDRAWALS_FAIL_MESSAGE);
				}
			}else {
				//提交的用户名与当前登录的用户id不属于同一个帐号
				return super.error(SystemCode.NOT_MATCH_ERROR, SystemCode.NOT_MATCH_MESSAGE, SystemCode.NOT_MATCH_MESSAGE);
			}
		}
			// 返回用户不存在
			return super.error(SystemCode.USER_INFO_NOT_EXIST_ERROR, SystemCode.USER_INFO_NOT_EXIST_MESSAGE, SystemCode.USER_INFO_NOT_EXIST_MESSAGE);

	}

}
